package com.qinglei.recoup.app.controller;

import com.qinglei.recoup.app.pojo.TagParam;
import com.qinglei.recoup.app.service.WxRadarService;
import com.qinglei.recoup.common.domain.CommonRes;
import com.qinglei.recoup.common.exception.RedisConnectException;
import com.qinglei.recoup.common.utils.JsonUtils;
import com.qinglei.recoup.system.domain.Bed;
import com.qinglei.recoup.system.domain.Radar;
import com.qinglei.recoup.system.pojo.RealTimeDataVO;
import com.qinglei.recoup.system.pojo.RealTimeDataVOV4;
import com.qinglei.recoup.system.service.BedService;
import com.qinglei.recoup.system.service.DictService;
import com.qinglei.recoup.system.service.RadarUserDataFourService;
import com.qinglei.recoup.system.service.RadarUserDataService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/weixin")
public class WxRadarController {

    @Autowired
    private WxRadarService wxRadarService;


    @Autowired
    private BedService bedService;

    @Autowired
    private RadarUserDataService radarUserDataService;

    @Autowired
    private DictService dictService;

    @Autowired
    private RadarUserDataFourService radarUserDataFourService;

    @GetMapping("getDefaultRadarData")
    public CommonRes getDefaultRadarData(String startDate,String endDate){
        return  wxRadarService.getDefaultRadarData(startDate,endDate);
    }

    @PostMapping("setDefaultRadarData")
    public CommonRes setDefaultRadarData(@RequestBody  TagParam tagParam){
        return  wxRadarService.setDefaultRadarData(tagParam);
    }


    @GetMapping("/getWeiXinLabel")
    public CommonRes getWeiXinLabel() throws RedisConnectException {
         return CommonRes.ok(dictService.getDictItemList("we_chat_label"));
    }



    @GetMapping("/gerWeixinRealTimeData")
    public CommonRes getRealTimeData(@NotNull(message = "{required}") String snCode, Integer second ) throws RedisConnectException {
        log.info("gerWeixinRealTimeData parm  snCode:{} , second:{}",snCode,second);
        Bed bed = bedService.getBedBySnNoTenantId(snCode);
        Map<String,Object> result = new HashMap<>();
        log.info("gerWeixinRealTimeData bed:{}", JsonUtils.objectToJson(bed));
        if(bed == null ){
            result.put("msgCode",1);
            result.put("msg","设备未绑定");
            return CommonRes.ok(result);

        }
        Integer bedId = bed.getId();

        if(StringUtils.isEmpty(bed.getRadarVersion())){
            result.put("msgCode",2);
            result.put("msg","设备绑定成功，但暂时收不到数据，请尝试重新连接电源并且配网");
            log.info("gerWeixinRealTimeData version is null :{}", result);
            return CommonRes.ok(result);
        }

        result.put("IsRadarOn",bed.getIsRadarOn());
        result.put("msgCode",0);
        result.put("msg","设备绑定成功,已接收到数据");
        log.info("getRealTimeData resultStop: {}",result);
         return CommonRes.ok(result);
    }
}
